<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/8/12
 * Time: 14:00
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Build extends Base
{
    /**
     * 楼盘列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $b_title = trim(input('b_title'), ' ');
            $b_is_sale = input('b_is_sale');
            $b_status = input('b_status');
            $b_tuijiang = input('b_tuijiang');
            $where = [];
            if(isset($b_title) && $b_title != ''){
                $where['b_title'] = ['like', '%' . $b_title . '%'];
            }
            if(isset($b_is_sale) && $b_is_sale != ''){
                $where['b_is_sale'] = $b_is_sale;
            }
            if(isset($b_status) && $b_status != ''){
                $where['b_status'] = $b_status;
            }
            if(isset($b_tuijiang) && $b_tuijiang != ''){
                $where['b_tuijiang'] = $b_tuijiang;
            }
            if(isset($params['sheng']) && $params['sheng'] != ''){
                $where['b_sheng'] = $params['sheng'];
            }
            if(isset($params['shi']) && $params['shi'] != ''){
                $where['b_shi'] = $params['shi'];
            }
            if(isset($params['qu']) && $params['qu'] != ''){
                $where['b_qu'] = $params['qu'];
            }
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('building')->where($where)->count();
            $lists = Db::name('building')->alias('u')
                ->join('up_area a1', 'u.b_sheng = a1.district_id', 'left')
                ->join('up_area a2', 'u.b_shi = a2.district_id', 'left')
                ->join('up_area a3', 'u.b_qu = a3.district_id', 'left')
                ->field('b_id, b_title, b_title_bottom, b_is_sale, b_hot_title, b_status, b_tuijiang, 
                b_time,a1.district sheng,a2.district shi,a3.district qu')
                ->where($where)
                ->page($page,$limit)
                ->order('b_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        // 省
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $this->assign([
            'province' => $province
        ]);
        return $this->fetch();
    }
    
    /**
     * 添加楼盘
     * @return mixed|\think\response\Json
     */
    public function buildAdd(){
        if(Request::instance()->isPost()){
            $b_title = trim(input('b_title'), ' ');
            $b_title_bottom = trim(input('b_title_bottom'), ' ');
            $b_hot_title = trim(input('b_hot_title'), ' ');
            $photo = input('photo');
            $b_mianji_min = input('b_mianji_min');
            $b_mianji_max = input('b_mianji_max');
            $b_price_max = input('b_price_max');
            $b_price = input('b_price');
            $b_unit_price = input('b_unit_price');
            $b_is_sale = input('b_is_sale');
            $b_shoufu = input('b_shoufu');
            $b_huxing = input('b_huxing');
            $b_sign = input('b_sign');
            $b_kaipan = input('b_kaipan');
            $b_jiaofang = input('b_jiaofang');
            $b_type = input('b_type');
            $b_kaifas = input('b_kaifas');
            $b_user_ids = input('b_user_ids');
            $b_sheng = input('b_sheng');
            $b_shi = input('b_shi');
            $b_qu = input('b_qu');
            $b_street = input('b_street');
            $b_address = input('b_address');
            $b_lon = input('b_lon');
            $b_lat = input('b_lat');
            if(empty($photo)){
                return json(['code' => 100, 'msg' => '请上传图片']);
            }
            if(empty($b_address) || empty($b_lon) || empty($b_lat)){
                return json(['code' => 100, 'msg' => '请填写详细地址']);
            }
            $img = current(explode(',', $photo));
            $dknl = Db::table('up_about')->where('id',4)->value('content');
            $mww_dk =  new \app\api\controller\Show();
            $res = $mww_dk->debx(360,($b_price - $b_shoufu)*10000,$dknl);
            $yuegong = $res['data'][0]['month'];
            $insert = [
                'b_title' => $b_title,
                'b_title_bottom' => $b_title_bottom,
                'b_img' => $img,
                'b_images' => $photo,
                'b_mianji_min' => $b_mianji_min,
                'b_mianji_max' => $b_mianji_max,
                'b_price' => $b_price,
                'b_price_max' => $b_price_max,
                'b_unit_price' => $b_unit_price,
                'b_is_sale' => $b_is_sale,
                'b_hot_title' => $b_hot_title,
                'b_yuegong' => $yuegong,
                'b_shoufu' => $b_shoufu,
                'b_huxing' => $b_huxing,
                'b_sign' => $b_sign,
                'b_kaipan' => $b_kaipan,
                'b_jiaofang' => $b_jiaofang,
                'b_type' => $b_type,
                'b_kaifas' => $b_kaifas,
                'b_user_ids' => $b_user_ids,
                'b_sheng' => $b_sheng,
                'b_shi' => $b_shi,
                'b_qu' => $b_qu,
                'b_street' => $b_street,
                'b_address' => $b_address,
                'b_lat' => $b_lat,
                'b_lon' => $b_lon,
                'b_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_building')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
            
        }
        // 标签
        $sign = Db::table('up_build_signs')
            ->field('bs_id, bs_name')
            ->order('bs_time desc')
            ->select();
        // 经纪人
        $jing = Db::table('up_u_user')
            ->where('user_type', 2)
            ->where('user_status', 1)
            ->field('user_id, user_username')
            ->select();
        // 省
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $this->assign([
            'sign' => $sign,
            'jing' => $jing,
            'province' => $province
        ]);
        return $this->fetch('build/buildadd');
            
    }
    
    /**
     * 楼盘详情
     * @return mixed
     */
    public function buildDetail(){
        $b_id = input('b_id');
        $build = Db::table('up_building')
            ->field('b_title, b_title_bottom, b_img, b_images, b_mianji_min, b_mianji_max, b_price, 
            b_price_max, b_unit_price, b_is_sale, b_hot_title, b_yuegong, b_shoufu, b_huxing, b_sign, 
            b_kaipan, b_jiaofang, b_type, b_kaifas, b_user_ids, b_address, b_status, b_tuijiang')
            ->where('b_id', $b_id)
            ->find();
        // 图片
        $imgs = explode(',', trim($build['b_images'], ','));
        $build['b_images'] = $imgs;
        // 经纪人
        $jing = Db::table('up_u_user')->where('user_id', 'IN', $build['b_user_ids'])->column('user_username');
        $build['b_user_ids'] = implode(',', $jing);
        // 标签
        $sign = Db::table('up_build_signs')->where('bs_id', 'IN', $build['b_sign'])->column('bs_name');
        $build['b_sign'] = implode(',', $sign);
        $this->assign('build', $build);
        return $this->fetch('build/builddetail');
    }
    
    /**
     * 编辑楼盘
     * @return mixed|\think\response\Json
     */
    public function buildEdit(){
        $b_id = input('b_id');
        if(Request::instance()->isPost()){
            $b_title = trim(input('b_title'), ' ');
            $b_title_bottom = trim(input('b_title_bottom'), ' ');
            $b_hot_title = trim(input('b_hot_title'), ' ');
            $photo = input('photo');
            $b_mianji_min = input('b_mianji_min');
            $b_mianji_max = input('b_mianji_max');
            $b_price_max = input('b_price_max');
            $b_price = input('b_price');
            $b_unit_price = input('b_unit_price');
            $b_is_sale = input('b_is_sale');
            $b_shoufu = input('b_shoufu');
            $b_huxing = input('b_huxing');
            $b_sign = input('b_sign');
            $b_kaipan = input('b_kaipan');
            $b_jiaofang = input('b_jiaofang');
            $b_type = input('b_type');
            $b_kaifas = input('b_kaifas');
            $b_user_ids = input('b_user_ids');
            $b_sheng = input('b_sheng');
            $b_shi = input('b_shi');
            $b_qu = input('b_qu');
            $b_street = input('b_street');
            $b_address = input('b_address');
            $b_lon = input('b_lon');
            $b_lat = input('b_lat');
            if(empty($photo)){
                return json(['code' => 100, 'msg' => '请上传图片']);
            }
            if(empty($b_address) || empty($b_lon) || empty($b_lat)){
                return json(['code' => 100, 'msg' => '请填写详细地址']);
            }
            $img = current(explode(',', $photo));
            $dknl = Db::table('up_about')->where('id',4)->value('content');
            $mww_dk =  new \app\api\controller\Show();
            $res = $mww_dk->debx(360,($b_price - $b_shoufu)*10000,$dknl);
            $yuegong = $res['data'][0]['month'];
            $update = [
                'b_title' => $b_title,
                'b_title_bottom' => $b_title_bottom,
                'b_img' => $img,
                'b_images' => $photo,
                'b_mianji_min' => $b_mianji_min,
                'b_mianji_max' => $b_mianji_max,
                'b_price' => $b_price,
                'b_price_max' => $b_price_max,
                'b_unit_price' => $b_unit_price,
                'b_is_sale' => $b_is_sale,
                'b_hot_title' => $b_hot_title,
                'b_yuegong' => $yuegong,
                'b_shoufu' => $b_shoufu,
                'b_huxing' => $b_huxing,
                'b_sign' => $b_sign,
                'b_kaipan' => $b_kaipan,
                'b_jiaofang' => $b_jiaofang,
                'b_type' => $b_type,
                'b_kaifas' => $b_kaifas,
                'b_user_ids' => $b_user_ids,
                'b_sheng' => $b_sheng,
                'b_shi' => $b_shi,
                'b_qu' => $b_qu,
                'b_street' => $b_street,
                'b_address' => $b_address,
                'b_lat' => $b_lat,
                'b_lon' => $b_lon,
                'b_updatetime' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_building')->where('b_id', $b_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $build = Db::table('up_building')
            ->where('b_id', $b_id)
            ->field('b_title, b_title_bottom, b_img, b_images, b_mianji_min, b_mianji_max, b_price, 
            b_price_max, b_unit_price, b_is_sale, b_hot_title, b_yuegong, b_shoufu, b_huxing, b_sign, 
            b_kaipan, b_jiaofang, b_type, b_kaifas, b_user_ids, b_sheng, b_shi, b_qu, b_street, b_address, 
            b_lat, b_lon')
            ->find();
        // 标签
        $sign = Db::table('up_build_signs')
            ->field('bs_id, bs_name')
            ->order('bs_time desc')
            ->select();
        // 经纪人
        $jing = Db::table('up_u_user')
            ->where('user_type', 2)
            ->where('user_status', 1)
            ->field('user_id, user_username')
            ->select();
        // 省
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        // 市
        $city = Db::table('up_area')
            ->where('level', 2)
            ->where('pid', $build['b_sheng'])
            ->field('district_id, district')
            ->select();
        // 区
        $district = Db::table('up_area')
            ->where('level', 3)
            ->where('pid', $build['b_shi'])
            ->field('district_id, district')
            ->select();
        // 街道
        $street = Db::table('up_area')
            ->where('level', 4)
            ->where('pid', $build['b_qu'])
            ->field('district_id, district')
            ->select();
        $this->assign([
           'build' => $build,
            'sign' => $sign,
            'jing' => $jing,
            'province' => $province,
            'city' => $city,
            'district' => $district,
            'street' => $street,
            'b_id' => $b_id
        ]);
        return $this->fetch('build/buildedit');
    }
    
    /**
     * 设为非在售
     * @return \think\response\Json
     */
    public function buildUnsale(){
        $b_id = input('id');
        $res = Db::table('up_building')->where('b_id', $b_id)->update(['b_is_sale' => 2]);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 设置为在售
     * @return \think\response\Json
     */
    public function buildSale(){
        $b_id = input('id');
        $res = Db::table('up_building')->where('b_id', $b_id)->update(['b_is_sale' => 1]);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 设置为下架
     * @return \think\response\Json
     */
    public function buildDown(){
        $b_id = input('id');
        $res = Db::table('up_building')->where('b_id', $b_id)->update(['b_status' => 2]);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 设置为上架
     * @return \think\response\Json
     */
    public function buildUp(){
        $b_id = input('id');
        $res = Db::table('up_building')->where('b_id', $b_id)->update(['b_status' => 1]);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 设置为推荐
     * @return \think\response\Json
     */
    public function tuiJian(){
        $b_id = input('id');
        $res = Db::table('up_building')->where('b_id', $b_id)->update(['b_tuijiang' => 2]);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 设置为非推荐
     * @return \think\response\Json
     */
    public function unTuijian(){
        $b_id = input('id');
        $res = Db::table('up_building')->where('b_id', $b_id)->update(['b_tuijiang' => 1]);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除楼盘
     * @return \think\response\Json
     */
    public function buildDel(){
        $b_id = input('id');
        $res = Db::table('up_building')->delete($b_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 楼盘户型图
     * @return mixed|\think\response\Json
     */
    public function buildImage(){
        if(request()->isAjax ()){
            $where['dh_dis_id'] = session('b_id');
            $where['dh_type'] = 2;
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('district_hu')->where($where)->count();
            $lists = Db::name('district_hu')
                ->field('dh_id, dh_name, dh_image, dh_time')
                ->where($where)
                ->page($page,$limit)
                ->order('dh_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        $b_id = input('b_id');
        if($b_id){
            session('b_id', $b_id);
        }
        return $this->fetch('build/buildimage');
    }
    
    /**
     * 添加户型图
     * @return mixed|\think\response\Json
     */
    public function addImage(){
        if(Request::instance()->isPost()){
            $dh_name = trim(input('dh_name'), ' ');
            $dh_image = input('dh_image');
            $insert = [
                'dh_dis_id' => session('b_id'),
                'dh_name' => $dh_name,
                'dh_image' => $dh_image,
                'dh_time' => date('Y-m-d H:i:s'),
                'dh_type' => 2
            ];
            $res = Db::table('up_district_hu')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('build/addimage');
    }
    
    /**
     * 编辑户型图
     * @return mixed|\think\response\Json
     */
    public function editImage(){
        $dh_id = input('dh_id');
        if(Request::instance()->isPost()){
            $dh_name = trim(input('dh_name'), ' ');
            $dh_image = input('dh_image');
            $update = [
                'dh_name' => $dh_name,
                'dh_image' => $dh_image,
                'dh_updatetime' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_district_hu')->where('dh_id', $dh_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $img = Db::table('up_district_hu')
            ->field('dh_name, dh_image')
            ->where('dh_id', $dh_id)
            ->find();
        $this->assign([
            'dh_id' => $dh_id,
            'img' => $img
        ]);
        return $this->fetch('build/editimage');
    }
    
    /**
     * 删除户型图
     * @return \think\response\Json
     */
    public function delImage(){
        $dh_id = input('id');
        $res = Db::table('up_district_hu')->delete($dh_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}